import collections
from random import choice
Card = collections.namedtuple('Card', ['rank', 'suit'])
class FrenchDeck:
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')
    suits = 'spades diamonds clubs hearts'.split()
    def __init__(self):
        self._cards = [Card(rank,suit) for suit in self.suits
                                       for rank in self.ranks]
    def __len__(self):
        return len(self._cards)
    def __getitem__(self, position):
        return self._cards[position]
  
berr_card = Card('7','diamonds')
print(berr_card.rank)
deck = FrenchDeck()
#数一数有几张牌
print(len(deck))
#取第一张牌
print(deck[0])
#取最后一张牌
print(deck[-1])
#随机选一个牌
print(choice(deck))
#从前往后数三个
print(deck[:3])
#从第12个开始，每隔13个拿一张牌
print(deck[12::13])
for card in deck:
    print(card)
print('\n')
for card in reversed(deck):
    print(card)

print(Card('Q','hearts') in deck )

suit_values = dict(spades = 3,hearts = 2,diamonds=1,clubs = 0)
def spades_high(card):
    rank_value = FrenchDeck.ranks.index(card.rank)
    return rank_value * len(suit_values) + suit_values[card.suit]

for card in sorted(deck,key=spades_high):
    print(card)